package com.hzlx.dao;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86166
 * @version 1.0
 * @description: TODO
 * @date 2023/4/9 22:34
 */
public class BaseDao<T> {
    public int executeUpdate(String sql,Object ... params){
        int rows = -1;
        Connection connection = ConnectionPool.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i+1,params[i]);
            }
            rows = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (preparedStatement!=null){
                    preparedStatement.close();
                }
                if (connection!=null){
                    ConnectionPool.returnConnection(connection);
                }
            } catch (SQLException e) {
              e.printStackTrace();
            }
        }
        return rows;
    }
    public List<T> executeQuery(String sql,Class aClass,Object ... params){
        List<T> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        connection = ConnectionPool.getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i+1,params[i]);
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Object obj = aClass.newInstance();
                Field[] declaredFields = aClass.getDeclaredFields();
                for (int i = 0; i < declaredFields.length; i++) {
                    declaredFields[i].setAccessible(true);
                    declaredFields[i].set(obj,resultSet.getObject(i+1));
                }
                list.add((T)obj);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }finally {
            try {
                if (preparedStatement!=null){
                    preparedStatement.close();
                }
                ConnectionPool.returnConnection(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    /**
     * 存储一行查询出的数据（没有对应实体类）
     * @author 张家奇
     * @date 2023/4/11 13:52
     * @param sql
     * @param params
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public static Map<String,Object> executeQueryMap(String sql,Object ... params){
           Connection connection = null;
           PreparedStatement preparedStatement  = null;
           connection = ConnectionPool.getConnection();
           Map<String,Object> map = new HashMap<>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i+1,params[i]);
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            resultSet.next();
            for (int i = 0; i < columnCount; i++) {
                map.put(metaData.getColumnLabel(i+1),resultSet.getObject(i+1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            ConnectionPool.returnConnection(connection);
            return map;
        }
    }
    /**
     * 存储多行查询出的数据（没有对应实体类）
     * @author 张家奇
     * @date 2023/4/11 13:52
     * @param sql
     * @param params
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public static List<Map<String,Object>> executeQueryList(String sql, Object ... params){
        Connection connection = ConnectionPool.getConnection();
        PreparedStatement preparedStatement = null;
        List<Map<String,Object>> list = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i+1,params[i]);
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            while (resultSet.next()) {
                Map<String,Object> map = new HashMap<>();
                for (int i = 0; i < columnCount; i++) {
                    map.put(metaData.getColumnLabel(i+1),resultSet.getObject(i+1));
                }
                list.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            ConnectionPool.returnConnection(connection);
        }
        return list;
    }
}
